package com.dawn.module.hospital.controller.app.outpatient;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSONObject;
import com.dawn.framework.apisign.core.annotation.Sign;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.web.core.util.RequestUtils;
import com.dawn.module.hospital.controller.admin.statics.vo.OutpatientRespVO;
import com.dawn.module.hospital.controller.admin.statics.vo.SearchReqVO;
import com.dawn.module.hospital.controller.app.outpatient.vo.*;
import com.dawn.module.hospital.enums.ErrorCodeConstants;
import com.dawn.module.hospital.service.OutpatientService;
import com.dawn.module.hospital.service.StaticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * @description:
 * @author: cnsu-cmh
 * @time: 2024/8/7 13:45
 */
@Tag(name = "门诊缴费 APP - bh")
@RestController
@RequestMapping("/bh/outpatient")
@Validated
public class AppOutpatientController {

    @Autowired
    private OutpatientService outpatientService;

    @Autowired
    private StaticsService staticsService;

    @Sign
    @PostMapping(value = "/register/list")
    @Operation(summary = "获取就诊记录列表")
    public CommonResult registerList(@Valid @RequestBody OutpatientRegisterQueryReqVO reqVO){
        return outpatientService.registerList(reqVO);
    }


    @Sign
    @Operation(summary = "获取待缴费单据列表")
    @PostMapping(value = "/unpay/list")
    public CommonResult unpayList(@Valid @RequestBody OutpatientUnpayQueryReqVO reqVO){
        return outpatientService.unPayList(reqVO);
    }

    @Sign
    @Operation(summary = "获取待缴费单据明细")
    @PostMapping(value = "/unpay/items")
    public CommonResult unpayItems(@Valid @RequestBody OutpatientUnpayQueryReqVO reqVO){
        return outpatientService.orderItems(reqVO);
    }

    @Operation(summary = "获取处方单二维码")
    @PostMapping(value = "/get/qrcode")
    public CommonResult getQrcode(@Valid @RequestBody OutpatientQueryReqVO reqVO) {
        return outpatientService.getQrcode(reqVO);
    }

    @Operation(summary = "门诊缴费创建订单")
    @PostMapping(value = "/create/order")
    public CommonResult createOrder(@Valid @RequestBody OutpatientCreateOrderReqVO reqVO) {
        return outpatientService.createOrder(reqVO);
    }

    @PostMapping(value = "/dopay")
    @Operation(summary = "门诊缴费下单")
    public CommonResult dopay(HttpServletRequest request) {
        JSONObject jsonObject = RequestUtils.getReqParamJSON(request);
        String orderNumber = jsonObject.getString("orderNumber");
        if (StringUtils.isEmpty(orderNumber)) {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(), "订单数据不存在或已失效");
        }
        return outpatientService.buildPaySignData(orderNumber);
    }

    @Sign
    @Operation(summary = "查询订单信息")
    @PostMapping(value = "/query/order")
    public CommonResult queryOrder(@Valid @RequestBody OutpatientOrderQueryReqVO reqVO) {
        return outpatientService.queryByOrderNumber(reqVO.getOrderNumber());
    }

    @Sign
    @Operation(summary = "门诊缴费记录")
    @PostMapping("/list")
    public CommonResult<PageResult<OutpatientRespVO>> pageOutpatient(@Valid @RequestBody SearchReqVO reqVO){

        if (reqVO.getSearchTime() != null && reqVO.getSearchTime().length > 1) {
            LocalDateTime beginDateTime = reqVO.getSearchTime()[0];
            LocalDateTime endDateTime = reqVO.getSearchTime()[1];
            reqVO.setBeginDate(LocalDateTimeUtil.formatNormal(beginDateTime));
            reqVO.setEndDate(LocalDateTimeUtil.formatNormal(endDateTime));
        }

        PageResult<OutpatientRespVO> pageResult = staticsService.getOutpatientPage(reqVO);

        return CommonResult.success(pageResult);
    }

    @Operation(summary = "获取历次就诊记录")
    @PostMapping(value = "/see/record")
    public CommonResult seeRecord(@Valid @RequestBody OutpatientQueryReqVO reqVO) {
        return outpatientService.seeRecord(reqVO);
    }

}
